package com.thoughtservice.jspdoc.output;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.gagawa.java.elements.Body;
import com.hp.gagawa.java.elements.Div;
import com.hp.gagawa.java.elements.Head;
import com.hp.gagawa.java.elements.Script;

public class GenerateD3Structure {
	
	private final static Logger LOG = LoggerFactory.getLogger(GenerateD3Structure.class);
			
	private Head head;
	private Body body;	
	
	public GenerateD3Structure(String json, String relativePath, int height) {
		LOG.trace("Entering {}", "GenerateD3Structure");
		head = createHead(relativePath);	
		body = createBody(json, height);
		LOG.trace("Exiting {}", "GenerateD3Structure");
	}

	/*public GenerateD3Structure(String json) {
		Html html = new Html();
		html.appendChild(createHead());
		html.appendChild(createBody(json));

		System.out.println(html.write());
		try {
			FileUtils.writeStringToFile(new File("generated.html"), html.write());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/

	public Head createHead(String relativePath) {			

		LOG.trace("Entering {}", "createHead");
		Head head = new Head();
		head.appendText("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n");
		head.appendText("<script type=\"text/javascript\" src=\""+relativePath+"resources/d3.v2.js\"></script>\n");
		head.appendText("<style type=\"text/css\">\n");

		head.appendText(".node rect {cursor: pointer; fill: #fff; fill-opacity: .5; stroke: #3182bd; stroke-width: 1.5px;}\n");
		head.appendText(".node text {font: 10px sans-serif; pointer-events: none;}\n");
		head.appendText("path.link {fill: none; stroke: #9ecae1; stroke-width: 1.5px;}\n");

		head.appendText("</style>");

		LOG.trace("Exiting {}", "createHead");
		return head;
	}

	private Body createBody(String json, int height) {
		LOG.trace("Entering {}", "creatBody");
		Body body = new Body();

		Div chart = new Div();
		chart.setId("chart");

		body.appendChild(chart);

		Script script = new Script("text/javascript");
		script.appendText("var myjson = '"+json+"';\n");
		try {
			
			script.appendText("var w = 960, h = "+height+", i = 0, barHeight = 20, barWidth = w * .8, duration = 400, root;");
			
			script.appendText(IOUtils.toString(GenerateD3Structure.class.getClassLoader()
					.getResourceAsStream("d3script.code")));

		} catch (IOException e) {
			LOG.error(ExceptionUtils.getFullStackTrace(e));
		}
		// script.appendText("var w = 960, h = 800, i = 0, barHeight = 20, barWidth = w * .8, duration = 400, root;");
		// script.appendText("var tree = d3.layout.tree().size([h, 100]);");

		body.appendChild(script);
		LOG.trace("Exiting {}", "createBody");
		return body;
	}

	public static void main(String[] args) {
		new GenerateD3Structure("","", 50);
	}

	public Head getHead() {
		return head;
	}

	public Body getBody() {
		return body;
	}
}
